package com.suxiaoxiao.springbootlearn.designpattern.commandpattern.commands.ceilingfan;

import com.suxiaoxiao.springbootlearn.designpattern.commandpattern.commands.Command;

/**
 * @author 、
 */
public abstract class BaseCeilingCommand implements Command {

    /**
     * 吊扇实例
     */
    protected CeilingFan ceilingFan;
    /**
     * 之前的速度，用于撤销操作。
     */
    protected int prevSpeed;

    /**
     * 执行命令
     */
    @Override
    public void execute() {
        prevSpeed = ceilingFan.getSpeed();
        subExecute();
    }

    /**
     * 子类具体的执行方法
     */
    protected abstract void subExecute();

    /**
     * 撤销通用命令
     */
    @Override
    public void undo() {
        if (prevSpeed == CeilingFan.HIGH) {
            ceilingFan.high();
        } else if (prevSpeed == CeilingFan.MEDIUM) {
            ceilingFan.medium();
        } else if (prevSpeed == CeilingFan.LOW) {
            ceilingFan.low();
        } else if (prevSpeed == CeilingFan.OFF) {
            ceilingFan.off();
        }
    }
}
